package cn.devpt.demo.service.impl;

import cn.devpt.demo.entity.PtDemoTable;
import cn.devpt.demo.service.PtDemoTableService;
import cn.devpt.dev.base.bean.PtMsg;
import cn.devpt.dev.base.bean.PtQuery;
import cn.devpt.dev.dao.JpaUtil;
import cn.devpt.dev.dao.QueryParams;
import cn.devpt.dev.base.bean.PtGrid;
import cn.devpt.dev.util.PtUtil;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 */
@Component
public class PtDemoTableServiceImpl implements PtDemoTableService {

    @Override
    public PtGrid find(PtQuery ptQuery) {
        String jpql = "select a from PtDemoTable a where 1=1 ";
        QueryParams params = new QueryParams();
        String anyQuery = ptQuery.getStr("anyQuery");
        if (PtUtil.strNotNull(anyQuery)) {
            jpql += " and a.name like :anyQuery";
            params.addParam("anyQuery", "%" + anyQuery + "%");
        }
        PtGrid list = JpaUtil.findAll(jpql, params, ptQuery);
        //   PtGrid list = this.findBySql(ptQuery);
        return list;
    }

    //该例子是用sql的写法,最后转换为对象(也可为Map,但是key是sql别名),适合复杂的查询
    private PtGrid findBySql(PtQuery ptQuery) {//sql的写法
        ptQuery.setSort(null);//这里只是demo,如果全用sql,前端PROP都得是数据库字段,jpa用的updTime,sql是UPD_TIME
        String sql = "select * from PT_DEMO_TABLE";
        QueryParams params = new QueryParams();
        String anyQuery = ptQuery.getStr("anyQuery");
        if (PtUtil.strNotNull(anyQuery)) {
            sql += " and a.name like ?";
            params.addParam("1", "%" + anyQuery + "%");
        }
        //JpaUtil.findBySql第3参数,如果未null,list.getRows()则返回List<Map> map里是sql对应的别名
        PtGrid list = JpaUtil.findBySql(sql, params, ptQuery, PtDemoTable.class);
        return list;
    }


    @Override
    public PtDemoTable findOne(String tableId) {
        PtDemoTable ptdemotable = JpaUtil.findById(PtDemoTable.class, tableId);
        return ptdemotable;
    }

    @Override
    @Transactional
    public void removeAll(List<PtDemoTable> ptdemotableLs) {
        for (PtDemoTable ptdemotable : ptdemotableLs) {
            JpaUtil.remove(PtDemoTable.class, ptdemotable.getTableId());
        }
    }

    @Override
    @Transactional
    public void remove(PtDemoTable table) {
        JpaUtil.remove(PtDemoTable.class, table.getTableId());
    }

    @Override
    @Transactional
    public PtMsg save(PtDemoTable ptdemotable) {
        if (PtUtil.strIsNull(ptdemotable.getTableId())) {
            ptdemotable.setTableId(PtUtil.genUuid());
            JpaUtil.create(ptdemotable);
        } else
            JpaUtil.update(ptdemotable);
        return PtUtil.genMsg(ptdemotable);

    }

    @Override
    @Transactional
    public PtMsg saveAll(List<PtDemoTable> ptdemotableLs) {
        for (PtDemoTable ptdemotable : ptdemotableLs) {
            this.save(ptdemotable);
        }
        return PtUtil.genMsg(ptdemotableLs);
    }

    @Override
    @Transactional
    public void addManyData(int startNum) {
        List<PtDemoTable> result = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            PtDemoTable table = new PtDemoTable();
            table.setName(startNum + i + 1 + "");
            table.setSorter(new Double(startNum + i + 1));
            table.setDateTime(PtUtil.addDay(PtUtil.getDateTime(), startNum));
            table.setDropGridId("36def74e-90eb-41d8-b741-17982ecfd253");//管理员的
            table.setDropCode(i % 4 + 1 + "");
            result.add(table);
        }
        this.saveAll(result);
    }

}

